package com.weather.wangs.weather.weather;

import android.content.Context;

import com.weather.wangs.weather.utils.UtilNetwork;

import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.KJLoger;

/**
 * Created by wangs on 2015/11/10.
 */
public class WeatherModelImpl implements WeatherModel {

    @Override
    public void getWeather(final Context context, final String url,
                           final WeatherOnFinishedListener listener) {
        if (!UtilNetwork.isNetworkConnected(context)) {
            KJLoger.debug("onError() tag=" + url);
            listener.onError(url, "网络连接失败");
            return;
        }
        KJHttp kjh = new KJHttp();
        HttpParams params = new HttpParams();
        params.putHeaders("Cookie", "cookie不能告诉你");
        params.put("name", "kymjs");
        kjh.post(url, params,
                new HttpCallBack() {

                    @Override
                    public void onSuccess(String t) {
                        super.onSuccess(t);
                        ViewInject.longToast(context, "请求成功");
                        KJLoger.debug("onSuccess() tag=" + url + "; \n " + t);
                        listener.onSuccess(url, t);
                    }

                    @Override
                    public void onFailure(int errorNo, String strMsg) {
                        super.onFailure(errorNo, strMsg);
                        ViewInject.longToast(context, "请求失败");
                        KJLoger.debug("onFailure() tag=" + url +
                                "; \n errorNo " + errorNo + "; strMsg:" + strMsg);
                        listener.onFailure(url, errorNo, strMsg);
                    }
                });
    }
}
